package demo.practice.leetcode_eveyday;

public class P28 {


    public static void main(String[] args) {
        P28 p28= new P28();
        p28.strStr("mississippi","pi");
    }
    public int strStr(String haystack, String needle) {

        if (needle == null || needle.length() == 0)
            return 0;

        int index = -1;
        for (int i = 0; i < haystack.length(); i++) {
            if (haystack.charAt(i) == needle.charAt(0)) {
                index = i;
                if (haystack.length() - i < needle.length()) {
                    return -1;
                }

                for (int j = 0; i < haystack.length()&& j<needle.length(); j++, i++) {
                    if (haystack.charAt(i) != needle.charAt(j)){
                        i= index;
                        index=-1;
                        break;
                    }

                    if(j==needle.length()-1)
                        return  index;
                }

            }
        }

        return index;
    }


}

